package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IComplainFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.CPOrderDetailModel;
import com.gears_software.sxdc.dccx.data.model.MyTripPassengerDetailModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.request.RComplainModel;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/8/2.
 */
@EActivity(R.layout.activity_complain)
public class ComplainActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView txt_phonenumber;
    @ViewById
    TextView txt_nickname;
    @ViewById
    TextView tv_chepaihao;
    @ViewById
    TextView tv_chexing;
    @ViewById
    TextView tv_color;
    @ViewById
    EditText edit_complain;
    @ViewById
    SimpleDraweeView img_avatar;
    @ViewById
    RelativeLayout re_carinfo;
    @Extra
    String id;
    @Extra
    String flag;
    @Extra
    MyTripPassengerDetailModel detailModel;
    @Extra
    CPOrderDetailModel orderDetailModel;
    @RestService
    IComplainFinder complainFinder;
    @Bean
    GearsErrorHandler errorHandler;


    @AfterViews
    void created() {
        actionbar_title.setText("投诉");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        complainFinder.setRestErrorHandler(errorHandler);
        if (flag != null && !flag.equals("")) {
            if (flag.equals("1")) {
                re_carinfo.setVisibility(View.GONE);
                if (orderDetailModel != null) {
                    if (orderDetailModel.getPassengeravatar() != null && !orderDetailModel.getPassengeravatar().equals("")){
                        Uri uri = Uri.parse(orderDetailModel.getPassengeravatar() + RequestUrl.avatar);
                        img_avatar.setImageURI(uri);
                    }
                    if (orderDetailModel.getPassengername() != null && !orderDetailModel.getPassengername().equals("")) {
                        txt_nickname.setText(orderDetailModel.getPassengername());
                    } else {
                        txt_nickname.setVisibility(View.GONE);
                    }
                    txt_phonenumber.setText(orderDetailModel.getPassengerphone());
                }
            } else if (flag.equals("2")) {
                re_carinfo.setVisibility(View.VISIBLE);
                if (detailModel != null) {
                    if (detailModel.getAvatar() != null && !detailModel.getAvatar().equals("")){
                        Uri uri = Uri.parse(detailModel.getAvatar() + RequestUrl.avatar);
                        img_avatar.setImageURI(uri);
                    }
                    if (detailModel.getDrivername() != null && !detailModel.getDrivername().equals("")) {
                        txt_nickname.setText(detailModel.getDrivername());
                        if (detailModel.getDrivername().equals(detailModel.getDriverphone())){
                            txt_phonenumber.setVisibility(View.GONE);
                        }else{
                            txt_phonenumber.setVisibility(View.VISIBLE);
                        }
                    } else {
                        txt_nickname.setVisibility(View.GONE);
                    }
                    txt_phonenumber.setText(detailModel.getDriverphone());
                    tv_chepaihao.setText(detailModel.getVehicleno());
                    tv_chexing.setText(detailModel.getCarmodel());
                    tv_color.setText(detailModel.getVehiclecolor());
                }
            }

        }
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
    }

    @Click
    void btn_submit(){
        if (edit_complain.getText().toString().equals("")){
            TastyToast.makeText(this,"请填写投诉信息",app.SHORT,app.WARNING);
        }else{
            RComplainModel complainModel = new RComplainModel();
            complainModel.setFlag(flag);
            complainModel.setDetail(edit_complain.getText().toString());
            complainModel.setId(id);
            submitComplainInfo(complainModel);
        }
    }

    @Background
    void submitComplainInfo(RComplainModel complainModel){
        ResultModel<String> res = complainFinder.complain(complainModel);
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<String> res){
        if (res != null){
            if (res.getErrno().equals("0")){
                TastyToast.makeText(this,"投诉信息提交成功",app.SHORT,app.SUCCESS);
                finish();
            }else{
                TastyToast.makeText(this,"投诉信息提交失败",app.SHORT,app.ERROR);
            }
        }

    }

    @Click
    void back_icon() {
        finish();
    }
}
